package bs

import net.liftweb._
import http._
import util._
import Helpers._
import scala.xml.{Text, NodeSeq}




trait DropdownItem {self: LiftComponent =>
  val dropdown_item_transformer: NodeSeq => NodeSeq = identity
}

object Divider extends LiftComponent with DropdownItem {
  override val dropdown_item_transformer = "* [class]" #> "divider"
  val template: NodeSeq = NodeSeq.Empty
}

case class Dropdown(title: String, menu: DropdownMenu) extends LiftComponent with DropdownItem with NavItem{

  override val template: NodeSeq = Templates("templates-hidden" :: "bs" :: "_dropdown" :: Nil).get

  override def transformer = ".dropdown-toggle >*" #> title &
    ".dropdown-menu" #> menu.render

  override val dropdown_item_transformer = ".dropdown [class]" #> "dropdown-submenu" &
    "b" #> NodeSeq.Empty andThen
    ".dropdown-toggle [data-toggle]" #> Option.empty[String] &
      ".dropdown-toggle [class!]" #> "dropdown-toggle" & ".dropdown-submenu ^^" #> "*"

  override val nav_item_transfer = ".dropdown ^^" #> "*"
}

case class DropdownMenu(items: LiftComponent with DropdownItem*) extends LiftComponent {

  override val template = Templates("templates-hidden" :: "bs" :: "_dropdown_menu" :: Nil).get

  override def transformer = "li" #> items.map { item =>
    "li *" #> item.render andThen "a [tabindex]" #> "-1" andThen item.dropdown_item_transformer
  }

}





